package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.RaceDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2014/11/3.
 */
@Controller
@RequestMapping("racedetail")
public class RaceDetailController {
    @Autowired
    private RaceDetailService raceDetailService;

    @RequestMapping("index")
    public String index(Model model,Integer raceId,HttpServletRequest request){

        Map<String,Object> detail = raceDetailService.getRaceDetailByRaceId(raceId);
        //任务信息
        model.addAttribute("detail",detail);
        //任务进程信息
        model.addAttribute("processes",raceDetailService.getProcesses());
        //是否竞标
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        if(userInfo !=null){

            //状态两种通过之后再加入这一句代码，不通过就不加入
            model.addAttribute("isbid",raceDetailService.getUserRaceManuscriptByRaceId(raceId,userInfo.getUserId()));
           //结束
            if(Integer.parseInt(detail.get("userId").toString()) == userInfo.getUserId() &&
                    Integer.parseInt(detail.get("progress").toString()) == 128105){
                model.addAttribute("prizeButton",1);
                model.addAttribute("prizes",raceDetailService.getPrizeByRaceId(raceId));

            }
            if(Integer.parseInt(detail.get("userId").toString()) == userInfo.getUserId()){
                model.addAttribute("isBaile",1);
            }
        }else{
            model.addAttribute("isbid", -1);
        }
        if(Integer.parseInt(detail.get("progress").toString())>=128106){
            return "/racedetail/finish";
        }
        return "/racedetail/index";
    }
    @RequestMapping(value = "getRaceManuscriptInfo",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getBidInfo(Integer page,Integer rows,
                                          Integer raceId){
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = raceDetailService.getRaceManuscript(raceId);
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return  map;
    }

    @RequestMapping(value = "getRaceManuscriptPrize",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getRaceManuscriptPrize(Integer page,Integer rows,
                                          Integer raceId){
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = raceDetailService.getRaceManuscriptPrize(raceId);
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return  map;
    }

    /**
     * 设置中奖
     * @param request
     * @param raceId 任务编号
     * @param ids 获奖者编号字符串
     * @return
     */
    @RequestMapping(value = "winRace",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> winRace(HttpServletRequest request,int raceId,String ids){
        Map<String, Object> map = new HashMap<String,Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        try {
            raceDetailService.updateWinRace(raceId,ids,userInfo.getUserId());
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return  map;
    }

}
